package ru.ognick.coachsystem.core;

import ru.ognick.coachsystem.R;
import ru.ognick.coachsystem.common.Exercise;
import ru.ognick.coachsystem.common.base.BaseExerciseView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CoreExerciseView extends BaseExerciseView {
    protected final String TAG = getClass().getSimpleName();
    
    private Chronometer chronometer;
    private ProgressBar progress;
    private RunStatus status;
    private long fixTime = 0; 
    
    public enum RunStatus {
    	RUN,
    	PAUSE,
    	STOP
    }
    
	public enum TimeStatus {
		NONE,
		BEEP,
		END
	}
    
	public CoreExerciseView(final Context context, Exercise exercise) {
		super(context, exercise);
	
		LayoutInflater inflator = ((Activity) context).getLayoutInflater();
		view = inflator.inflate(R.layout.core_exercise, null, true);
		
		TextView name = (TextView) view.findViewById(R.id.nameText);
		name.setText(exercise.getName());
		
		TextView description = (TextView) view.findViewById(R.id.descriptionText);
		description.setText(exercise.getDescription());
		
		chronometer = (Chronometer) view.findViewById(R.id.chronometer);
		progress = (ProgressBar) view.findViewById(R.id.progressBar);
		progress.setMax((int) exercise.getDuration().toSeconds());
		
		ifAfter();
	}
	
	public Chronometer getChronometer() {
		return chronometer;
	}
	
	private void restartTime() {
		progress.setProgress(0);
		chronometer.setBase(SystemClock.elapsedRealtime());
		chronometer.start();
	}
	
	private void stopTime() {
		status = RunStatus.STOP;
		chronometer.stop();
		chronometer.invalidate();
	}
	
	private void setContentColor(int color){
		LinearLayout content = (LinearLayout) view;
		if (content != null)
			for (int i=0; i<content.getChildCount(); i++) {
				try {
					TextView text = (TextView) ((LinearLayout) view).getChildAt(i);
					text.setTextColor(color);
				}
				catch (ClassCastException e) { 
				}
			}
		
	}
	
	private void setFixTime() {
		fixTime = SystemClock.elapsedRealtime() - chronometer.getBase();
		Log.d(TAG, "!!!!!!!!!!!!!! \n\n\n !!! FIXTIME " + fixTime);
	}
	
	
	public SavedState getSavedState() {
		switch (status) {
		case  RUN:
			return new SavedState(status, chronometer.getBase());
		case PAUSE:
			return new SavedState(status, fixTime);
		default:
			return null;
		}
	}
	
	public void setSaveState(SavedState savedState) {
		switch (savedState.getStatus()) {
		case RUN:
				status = RunStatus.PAUSE;
				fixTime = SystemClock.elapsedRealtime() - savedState.getBase();
				progress.setProgress((int) (fixTime / 1000));
			break;
		default:
				chronometer.setBase(SystemClock.elapsedRealtime() - savedState.getBase());
				status = RunStatus.RUN;
			break;
		}
	}
	

	public void onClick() {
		switch (status) {
		case PAUSE:
			status = RunStatus.RUN;
			setContentColor(Color.GREEN);
			chronometer.setBase(SystemClock.elapsedRealtime() - fixTime);
			chronometer.start();
			break;
		case RUN:
			setContentColor(Color.RED);
			status = RunStatus.PAUSE;
			chronometer.stop();
			setFixTime();
			break;			
		case STOP:
			status = RunStatus.RUN;
			setContentColor(Color.GREEN);
			restartTime();
			break;
		} 
	}
	
	public void ifBefore() {
		chronometer.setBase(SystemClock.elapsedRealtime());
		progress.setProgress(progress.getMax());
		stopTime();
		setContentColor(Color.GRAY);
	}
	
	public void ifAfter() {
		stopTime();
		progress.setProgress(0);
		chronometer.setBase(SystemClock.elapsedRealtime());
		setContentColor(Color.BLUE);
	}
	
	public TimeStatus checkTime(){
		long time = SystemClock.elapsedRealtime() - chronometer.getBase();
		int curSeconds = (int) (time / 1000);
		if (curSeconds == 0)
			return TimeStatus.NONE;
	
		progress.setProgress(curSeconds);
		
		if (exercise.isActivity() &&
				curSeconds >= exercise.getDuration().toSeconds()) 
				return TimeStatus.END;

		if (exercise.isBeep() &&  
		   curSeconds % exercise.getBeepPeriod().toSeconds() == 0) 
				return TimeStatus.BEEP;
		
		return TimeStatus.NONE;
	}
	
}

